package com.richardjaap.data;

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class RouletteTable {

	private int tableId;
	private int amountOfPlayers;
	private ArrayList<Player> players;
	private int roundsInactive = 0;
	
	public RouletteTable(int id){
		this.players = new ArrayList<Player>();
		amountOfPlayers = 0;
		this.tableId = id;
	}
	
	public boolean buyIn(int playerId, double buyIn){
		Account account = getAccount(playerId);
		double cashTotal = account.getCashTotal();
		if(cashTotal < buyIn){
			return false;
		} else {
			account.setCashTotal(cashTotal -= buyIn);
			return true;
		}
	}
	
	private Account getAccount(int id){
		for(Player player : players){
			if(player.getAccount().getId() == id){
				return player.getAccount();
			}
		}
		return null;
	}

	public int getTableId() {
		return tableId;
	}

	public ArrayList<Player> getPlayers() {
		return players;
	}
	
	public Player getPlayer(int id){
		for(Player player : players){
			if(player.getId() == id){
				return player;
			}
		}
		return null;
	}

	public void addPlayer(Player player) {
		if(!checkIfFull(amountOfPlayers)){
			players.add(player);
			amountOfPlayers++;
		} else {
			System.out.println("The table is currently full");
		}
	}
	
	public void removePlayer(int id){
		for(Player player : players){
			if(player.getId() == id){
				players.remove(player);
				amountOfPlayers--;
			}
		}
	}
	
	private boolean checkIfFull(int amount){
		if(amount == 10){
			return true;
		} else
		return false;
	}
	
	
	
	/*
	 * TODO Moet allemaal nog echt gemacht worden. Simpele uitwerking van rare ideeen
	 */
	
	public void startRound(){
		roundsInactive++;
		
		if(roundsInactive == 3){
			removePlayer(/*playerId*/0);
		}
	}
	
	public boolean placeBet(Bet bet){
		roundsInactive = 0;
		return false;
	}
	
	
}
